<template>
  <a-modal
    :open="open"
    width="860px"
    title="扫码移除"
    centered
    :ok-button-props="{ disabled: tableData.length === 0 }"
    ok-text="确定"
    @ok="onOk"
    @cancel="onCancel"
  >
    <div>
      <a-input v-model:value="tcCode" class="w-full mb-2" placeholder="请扫码" maxlength="20" @keydown.enter="getData()" />
      <div class="max-h-[560px] overflow-y-scroll">
        <BaseTable :data-source="tableData" column-code="000756" :loading="loading" row-key="lsh">
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'mc'">
              <MedicineInfo :value="record" />
            </template>
            <template v-if="column.dataIndex === 'action'">
              <BaseButton confirm-text="" type="link" size="small" danger @click="onDelete(record)">删除</BaseButton>
            </template>
          </template>
        </BaseTable>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  mode: {
    type: String,
    default: ''
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const loading = ref(false)
const tcCode = ref('')
const tableData = ref([])
const repeatData = ref([])

const getData = async () => {
  const { code, msg, data } = await post('/drugStorage/psiform/getTcCodeDrugList', {
    formSn: props.rowData.formSn,
    tcCode: tcCode.value,
    pageSize: 1,
    pageNum: 1
  })
  loading.value = false
  if (code !== 0) return message.error(msg || '操作失败')
  if (data && data.length > 0) {
    if (data.length !== 1) return message.error('获取该药品信息错误')
    repeatData.value = []
    repeatData.value = tableData.value.filter((item) => {
      return item.mc === data[0]?.mc && item.gg === data[0]?.gg && item.cj === data[0]?.cj && item.tcCode === data[0]?.tcCode
    })
    if (repeatData.value.length > 0) return message.error('该追溯码已存在，禁止重复。')
    tableData.value = [...tableData.value, ...data]
  } else {
    let msgData = ''
    if (props.mode === '1') msgData = '转移'
    if (props.mode === '3') msgData = '报损'
    if (props.mode === '10') msgData = '出库'

    message.warning(`该药品不在当前${msgData}药品的追溯码范围内，请重新扫码。`)
  }
}

const onDelete = async (record) => {
  tableData.value = tableData.value.filter((item) => {
    return item.tcCode !== record.tcCode
  })
}

const onCancel = () => {
  tableData.value = []
  tcCode.value = ''
  emit('update:open', false)
}
const onOk = async () => {
  if (tableData.value.length > 0) {
    const tcData = tableData.value.map((item) => {
      return item.tcCode
    })
    const { code, msg } = await post('/drugStorage/psiform/doDeleteTcCodeDrug', {
      formSn: props.rowData.formSn,
      operationMode: props.mode,
      tcCodes: tcData
    })
    if (code !== 0) return message.error(msg || '操作失败')
  }
  onCancel()
  emit('ok')
}
</script>
<style lang="less" scoped></style>
